import React from "react";
import BaseComp from "../Base/BaseComp";
import ModalComp from "../ModalItem/ModalComp";
import {taskFailCauseApi} from "../Config/GlobalApi";
import PropTypes from "prop-types";
import {map} from 'lodash-es'
// import TableComp from "../TableComp";
import {formatTip} from "../../tipConfig";
import {getRandomKey} from "jh-utils";

/**
 * 异步任务错误信息-弹框
 */
export default class TaskFailCauseModal extends BaseComp {

    static propTypes = {
        ...BaseComp.propTypes,
        appins: PropTypes.object,
        statusKey: PropTypes.string,
        taskGroup: PropTypes.string,
        // failCauseType: PropTypes.string,
        visible: PropTypes.bool,
        onCancel: PropTypes.func
    };

    searchData = {current: 1, pageSize: 40};

    colsConf = [
        {remark: '#', name: 'sign', align: 'center'},
        {remark: '错误信息', name: 'cause', render: (value, record) => formatTip(record.status, record.parameters)},
    ];

    initState(props) {
        return {
            ...super.initState(props),
            list: [],
            page: {
                total: 0, // 总记录数
                pageSize: 80, // 每页显示的记录数
                current: 1, // 当前页
                pages: 0, // 总页数
            }
        };
    }

    componentDidMount() {
        super.componentDidMount();
        this.fetchList();
    }

    fetchList() {
        let {current, pageSize} = this.searchData;
        const apiData = {
            statusKey: this.props.statusKey,
            taskGroup: this.props.taskGroup,
            // causeType: 'com.jianhui.dto.ResponseData', //this.props.failCauseType,
            start: 0,
            fetchSize: pageSize || 80
        };
        if (current) {
            apiData.start = (pageSize * (current - 1)) || 0;
        }

        // console.log('TaskFail apiData', apiData)

        if (apiData.statusKey && apiData.taskGroup) {
            this.setState({loading: true});
            return this.request(taskFailCauseApi, apiData).then(rsp => {
                const newState = {loading: false};

                if (rsp && 200 === rsp.status && rsp.data) {
                    newState.list = map(rsp.data.causes, (item, ind) => ({
                        ...item,
                        key: getRandomKey()
                    }));

                    newState.page = {
                        total: rsp.data.count,
                        pageSize: apiData.fetchSize,
                        current: current
                    };
                }
                this.setState(newState);
                return rsp;
            });
        }
    }

    /**
     * TableComp 组件有操作变化时的回调
     * @param pagination
     */
    handleChangeTable(pagination) {
        const {current, pageSize} = pagination;
        this.searchData.current = current;
        this.searchData.pageSize = pageSize;
        this.fetchList();
    }

    mainRender(context) {

        return (
            <ModalComp title={this.formatMsgByCn('错误信息')}
                       width={900}
                       visible={this.props.visible}
                       onCancel={this.props.onCancel}
                       footer={false}
            >
                {/*<TableComp*/}
                {/*    style={{height: 600}}*/}
                {/*    colsConf={this.colsConf}*/}
                {/*    dataSource={this.state.list}*/}
                {/*    loading={this.state.loading}*/}
                {/*    pagination={{*/}
                {/*        position: ['bottomCenter'],*/}
                {/*        ...this.state.page*/}
                {/*    }}*/}
                {/*    needNO={false}*/}
                {/*    onChange={this.handleChangeTable.bind(this)}*/}
                {/*/>*/}
            </ModalComp>
        )
    }
}